package com.star.sync.elasticsearch.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.star.sync.elasticsearch.service.ElasticsearchService;
import com.star.sync.elasticsearch.util.JsonUtil;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author <a href="mailto:wangchao.star@gmail.com">wangchao</a>
 * @version 1.0
 * @since 2017-08-27 12:38:00
 */
@Service
public class ElasticsearchServiceImpl implements ElasticsearchService {
    private static final Logger logger = LoggerFactory.getLogger(ElasticsearchServiceImpl.class);

    @Autowired
    @Qualifier("client")
    private RestHighLevelClient restHighLevelClient;


    @Override
    public void insertById(String index, String type, String id, Map<String, Object> dataMap) {
        IndexRequest request = new IndexRequest(index).id(id).source(dataMap);
       try {
           restHighLevelClient.index(request, RequestOptions.DEFAULT);
       }catch (Exception e){
           logger.error("插入异常{}", JSONObject.toJSONString(e));
       }
//        transportClient.prepareIndex(index, type, id).setSource(dataMap).get();
    }

    @Override
    public void batchInsertById(String index, String type, Map<String, Map<String, Object>> idDataMap) {
        BulkRequest request = new BulkRequest();

        idDataMap.forEach((id, dataMap) -> {
            IndexRequest req = new IndexRequest(index).id(id).source(dataMap);
            request.add(req);
        });
        try {
            BulkResponse bulkResponse = restHighLevelClient.bulk(request,RequestOptions.DEFAULT);
            if (bulkResponse.hasFailures()) {
                logger.error("elasticsearch批量插入错误, index=" + index + ", type=" + type + ", data=" + JsonUtil.toJson(idDataMap) + ", cause:" + bulkResponse.buildFailureMessage());
            }
        } catch (Exception e) {
            logger.error("elasticsearch批量插入错误, index=" + index + ", type=" + type + ", data=" + JsonUtil.toJson(idDataMap), e);
        }
    }

    @Override
    public void update(String index, String type, String id, Map<String, Object> dataMap) {
        this.insertById(index, type, id, dataMap);
    }

    @Override
    public void deleteById(String index, String type, String id) {
        DeleteRequest deleteRequest=new DeleteRequest();
        deleteRequest.index(index);
        deleteRequest.id(id);
        try {
            restHighLevelClient.delete(deleteRequest,RequestOptions.DEFAULT);
        }catch (Exception e){
            logger.error("删除异常{}", JSONObject.toJSONString(e));
        }

    }
}
